<template>
	<view class="selectDevice">
		<!-- 已选中的设备 -->
		<navBar title="已选设备" goBackUrl="/subpackage-equipment/pages/device/deviceCome/components/comeUnit" @goBack="goBack"></navBar>
		
		<text class="del" @click="getDelDevice">删除</text>
		
		<scroll-view scroll-y="true" class="scroll-box">
			<view class="content">
				<view class="info">
					<checkbox-group @change="checkboxChange">
						<label v-for="(item,index) in deviceList" :key="index" class="label">
							<view class="item" >
								<view class="header">{{ item.deviceTypeName }}/{{ item.subDeviceTypeName }}</view>
								<view class="item-body">
									<view class="item-nav">
										<view class="name">设备编号：</view>
										<view class="value">{{ item.code }}</view>
									</view>
									<view class="item-nav">
										<view class="name">规格型号：</view>
										<view class="value">{{ item.specificationModel }}</view>
									</view>
									<!-- <view class="item-nav">
										<view class="name">存储区域：</view>
										<view class="value">{{ item.storageAreaName }}</view>
									</view> -->
									<view class="item-nav">
										<view class="name">基地名称：</view>
										<view class="value">{{ item.stationName }}</view>
									</view>
									<checkbox :value="item.isShowSelect" :checked="item.isChecked" class="color round checkbox" />
								</view>
							</view>
						</label>
					</checkbox-group>
				</view>
			</view>
		</scroll-view>
		
		
		<view class="fixedButton">
			<u-button type="primary"  :loading="loading" text="保存" @click="getSave"></u-button>
		</view>
		<!-- <view class="button" @click="getSave">保存</view> -->
	</view>
</template>

<script>
	import navBar from "@/components/navBar/navBar.vue"
	import { mapMutations, mapState } from "vuex"
	export default {
		name: "selectDevice",
		components: {
			navBar
		},
		data() {
			return {
				selectArr: [],   // 选中的设备
				deviceList: [],
				loading: false,
			}
		},
		computed: {
			// 获取存储在vuex 中的数据
			// deviceComeSelcetList 选中的数据
			// checkBoxSelectList  checkbox选中的项值为 id数组
			...mapState('device',['deviceComeSelcetList', 'checkBoxSelectList'])
		},
		mounted() {
			this.deviceList = this.deviceComeSelcetList.map(item=>({...item, isChecked: false}))
		},
		methods: {
			...mapMutations('device', ["getDeviceComeSelcetList", "getCheckBoxSelectList"]),
			checkboxChange(e){
				console.log(e.detail.value)
				this.selectArr = e.detail.value
				this.deviceList.forEach(item=>{
					if(this.selectArr.includes(item.id)){
						this.$set(item, 'isChecked', true)
					}else{
						this.$set(item, 'isChecked', false)
					}
				})
			},
			
			// 删除选中的设备
			getDelDevice(){
				if(this.deviceList){
					this.deviceList = this.deviceList.filter(item=>{
						return !this.selectArr.find(value=> item.id === value)
					})
					let list = this.checkBoxSelectList.filter(item=>{
						return !this.selectArr.find(value=> item === value)
					})
					this.getDeviceComeSelcetList(this.deviceList)
					this.getCheckBoxSelectList(list)
				}
			},
			
			getSave(){
				if(this.loading) return
				this.loading = true
				uni.redirectTo({
					url: "/subpackage-equipment/pages/device/deviceCome/deviceCome"
				})
				this.loading = false
			},
			
			goBack(url){
				// uni.redirectTo({
				// 	url: url
				// })
				uni.navigateBack({})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.selectDevice{
		padding-bottom: 80rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		position: relative;
		/* #ifdef MP-WEIXIN */
			padding-top: 180rpx;
		/* #endif */
		.scroll-box{
			max-height: calc(100vh - 250rpx);
			padding-top: 10rpx;
		}
		.content{
			background-color: #F5F7FA;
			padding: 24rpx 32rpx;
		}
		.del{
			position: absolute;
			top: 42rpx;
			right: 50rpx;
			font-size: 32rpx;
			font-weight: 500;
			color: #FF6A6A;
		}
		.info{
			.item{
				width: 100%;
				background: #FFFFFF;
				margin-bottom: 20rpx;
				.header{
					font-size: 30rpx;
					line-height: 88rpx;
					font-weight: bold;
					color: #445469;
					height: 88rpx;
					border-bottom: 2rpx solid #E2E5ED;
					padding-left: 35rpx;
				}
				.item-body{
					position: relative;
					padding: 26rpx 35rpx;
					padding-bottom: 10rpx;
					
					.item-nav{
						display: flex;
						font-size: 28rpx;
						font-weight: 500;
						color: #445469;
						margin-bottom: 18rpx;
						.name{
							width: 160rpx;
						}
						.value{
							flex: 1;
						}
					}
				}
			}
			.checkbox{
				position: absolute;
				top: 25rpx;
				right: 25rpx;
			}
		}
		
		.button{
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			height: 105rpx;
			line-height: 105rpx;
			color: #FFFFFF;
			font-size: 32rpx;
			font-weight: 500;
			background-color: #2F80ED;
			text-align: center;
		}
	}
</style>
